package edu.ccut.saturn.manager.systeminfo.core;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import edu.ccut.saturn.component.DefaultComponent;
import edu.ccut.saturn.component.IParameterContext;

public class GetMemoryInfo extends DefaultComponent {
	@Override
	public void execute(IParameterContext parameterContext) throws Exception {
		
		Runtime rt = Runtime.getRuntime();
	
		String TotalMemory = String.valueOf(rt.totalMemory());
		String FreeMemory = String.valueOf(rt.freeMemory());
		String UsedMemory = String.valueOf(rt.totalMemory()-rt.freeMemory());
		String MemoryGraph = null;		
		
		List<String> keyList = new ArrayList<String>(); 

		Map<String, String> systemInfos = new HashMap<String, String>();	
		
		systemInfos.put("TotalMemory", TotalMemory);		
		systemInfos.put("FreeMemory", FreeMemory);
		systemInfos.put("UsedMemory", UsedMemory);
		systemInfos.put("MemoryGraph", MemoryGraph);
		
		for(String key : systemInfos.keySet()) {
			keyList.add(key);
		}		
		
		List<Map<String,String>> resultList = new ArrayList<Map<String,String>>();
		
		for(String key : keyList){
			
			Map<String, String> map = new HashMap<String, String>();
			
			map.put("key", key);
			map.put("value",systemInfos.get(key));
			
			resultList.add(map);
		}
		
		parameterContext.setParameter(0, resultList);
	}
}